//
//  ZJAddAddressViewController.m
//  ZiJinLian
//
//  Created by lc on 2017/3/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJAddAddressViewController.h"
#import "ZJCommItem.h"
#import "ZJAdressModel.h"
#import "ZJDeliveryAddressViewController.h"
#import "AddressPickerView.h"
@interface ZJAddAddressViewController ()<AddressPickerViewDelegate>


@property (nonatomic ,strong) AddressPickerView * pickerView;
@property (nonatomic ,strong) NSString * province;
@property (nonatomic ,strong) NSString * city;
@property (nonatomic ,strong) NSString * district;
@end

@implementation ZJAddAddressViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden=NO;
}

-(void)createView{
    [self initTitle:MyString(@"添加新地址")];
    [self.view addSubview:self.pickerView];
    _addressView=[[ZJAddAddressView alloc]init];
    [ZJNavTool rightItemOnNavigationItem:self target:self action:@selector(save) andType:item_save];
    [self.view addSubview:_addressView];
    [_addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(self.view.mas_top);
        make.width.equalTo(self.view.mas_width);
        make.height.mas_equalTo(260);
    }];
    if (_isEdit&&_model) {
        _addressView.peopleItem.textField.text=_model.consigneeName;
        _addressView.phoneItem.textField.text=_model.consigneePhone;
        [_addressView.setDefaultItem.checkBox setSelected:_model.isDefault==1];
        _addressView.aearItem.secondLab.text=_model.area;
        _addressView.addressDetailItem.textField.text=_model.detail;
    }
    
    
}



-(void)doOpeartion{
    [self doKeyBoardDown];
    UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(areaClick)];
    UITapGestureRecognizer*tapGesture2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(btnClick)];
    [_addressView.aearItem addGestureRecognizer:tapGesture];
    [_addressView.setDefaultItem addGestureRecognizer:tapGesture2];
}
- (AddressPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[AddressPickerView alloc]initWithFrame:CGRectMake(0, MainH-64  , MainW, 215)];
        _pickerView.delegate = self;
    }
    return _pickerView;
}

-(void)areaClick{
    [self.addressView endEditing:YES];
    [self.pickerView show];
}
#pragma mark - AddressPickerViewDelegate
- (void)cancelBtnClick{
    NSLog(@"点击了取消按钮");
    [self.pickerView hide];
    //[self btnClick:_addressBtn];
}
- (void)sureBtnClickReturnProvince:(NSString *)province City:(NSString *)city Area:(NSString *)area{
    _addressView.aearItem.secondLab.text=[NSString stringWithFormat:@"%@%@%@",province,city,area];
    self.province=province;
    self.city=city;
    self.district=area;
    [self.pickerView hide];
    //[self btnClick:_addressBtn];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [self.pickerView hide];
}

-(void)createAddressList{
    
    
}


-(void)btnClick{
    [_addressView.setDefaultItem.checkBox setSelected:!_addressView.setDefaultItem.checkBox.isSelected];
    
}

-(void)save{
    if (_addressView.peopleItem.textField.text.length==0) {
        [self showMessage:MyString(@"请输入收货人姓名")];
        return;
    }
    if (_addressView.phoneItem.textField.text.length!=11) {
        [self showMessage:MyString(@"请输入正确的收货人电话")];
        return;
    }
    if (_addressView.addressDetailItem.textField.text.length==0) {
        [self showMessage:MyString(@"请输入详细地址")];
        return;
    }
    
    
    //    if (_addressView.aearItem.secondLab.text.length==0) {
    //        [self showMessage:@"请选择省市区县"];
    //        return;
    //    }
    
    
    NSMutableDictionary* dic =[[NSMutableDictionary alloc]init];
    [dic setObject:[ZJUserDefaultsTools getCurrentUserID] forKey:USER_ID];
    [dic setObject:_addressView.peopleItem.textField.text forKey:@"consigneeName"];
    [dic setObject:_addressView.phoneItem.textField.text forKey:@"consigneePhone"];
    if (self.province==nil) {
        self.province=self.model.province;
        self.city=self.model.city;
        self.district=self.model.district;
    }
    [dic setObject:self.province forKey:@"province"];
    [dic setObject:self.city forKey:@"city"];
    [dic setObject:self.district forKey:@"district"];
    [dic setObject:_addressView.addressDetailItem.textField.text forKey:@"detail"];
    [dic setObject:_addressView.setDefaultItem.checkBox.selected? [[NSNumber alloc]initWithInt:1]: [[NSNumber alloc]initWithInt:0]forKey:@"isDefault"];
    if (_isEdit) {
        [dic setObject:_model.addressId forKey:@"addressId"];
    }
    NSString* url=_isEdit?API_EDIT_ADDRESS:API_ADD_ADDRESS;
    HTTPMethod method=_isEdit?PUT:POST;
    NSLog(@"%@",dic);
    [self doHttpRequestWithApi:url method:method dic:dic successBlock:^(NSDictionary *dic) {
        [self showToast:MyString(@"操作成功")];
        ZJAdressModel* modle =[[ZJAdressModel alloc]init];
        modle.consigneeName=_addressView.peopleItem.textField.text;
        modle.consigneePhone=_addressView.phoneItem.textField.text;
        modle.area=_addressView.aearItem.secondLab.text;
        modle.detail=_addressView.addressDetailItem.textField.text;
        modle.isDefault=_addressView.setDefaultItem.checkBox.selected?1:0;
        if (self.addData) {
            self.addData(modle);
        }
		
        [self.navigationController popViewControllerAnimated:YES];
		
    }];
    
    
}
    
@end
